package edu.vt.ece3574.ninjatag;

/****************************************************************************
 * Copyright 2012 Landon Biggs, Chris Dusold, Brad Russell, and Craig Wertz	*
 * Licensed under the Apache License, Version 2.0 (the "License");			*
 * you may not use this file except in compliance with the License.			*
 * You may obtain a copy of the License at									*
 * 																			*
 * http://www.apache.org/licenses/LICENSE-2.0								*
 * 																			*
 * Unless required by applicable law or agreed to in writing, software		*
 * distributed under the License is distributed on an "AS IS" BASIS,		*
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.	*
 * See the License for the specific language governing permissions and		*
 * limitations under the License.											*
 ****************************************************************************/

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.android.Utility;


public class games_screen extends Activity implements OnClickListener, OnItemClickListener{

	private Button join, newGame;
	private ListView gamesList;
	private String[] listOfGames;
	private String gameSelected;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.games_screen);

		join = (Button)findViewById(R.id.joinButton);
		newGame = (Button)findViewById(R.id.newGameButton);

		join.setOnClickListener(this);
		newGame.setOnClickListener(this);

		listOfGames = new String[]{"Filler1", "Filler2"};




		gamesList = (ListView)findViewById(R.id.games_list);
		gamesList.setOnItemClickListener(this);



		//TODO: Get listView to display Games.

		listOfGames = Utility.mGames.getNameList();


		gamesList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listOfGames));
		gameSelected = "0";
	}

	/*
	 *	The next couple methods are used to override the menu and back physical buttons on the phone.
	 *	onCreateOptionsMenu sets the menu to the custom 'main_menu'
	 *	onOptionsItemSelected starts an activity based on the selected option in the menu
	 * 	onBackPressed sends the app to the previous screen.
	 * 
	 */

	@Override
	public void onBackPressed(){
		Intent newIntent = new Intent(games_screen.this, group_screen.class);
		startActivity(newIntent);
		return;
	}

	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch(item.getItemId() ){

		case R.id.menu_home:
			Intent mainScreenIntent = new Intent(games_screen.this, main_screen.class);
			startActivity(mainScreenIntent);
			finish();
			break;


		case R.id.menu_map:
			Intent mapScreenIntent = new Intent(games_screen.this, map_screen.class);
			startActivity(mapScreenIntent);
			finish();
			break;

		case R.id.menu_settings:
			Intent settingsScreenIntent = new Intent(games_screen.this, settings_screen.class);
			startActivity(settingsScreenIntent);
			finish();
			break;
			
    	case R.id.gps_settings:
    		Intent GPS_ActivityIntent = new Intent(games_screen.this, GPS_Activity.class);
    		startActivity(GPS_ActivityIntent);
    		finish();
    		break;

		case R.id.menu_lock_or:

			if(getOrientation() == Configuration.ORIENTATION_PORTRAIT){
				this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
				break;
			}

			else if(getOrientation() == Configuration.ORIENTATION_LANDSCAPE){
				this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
				break;
			}
			else Toast.makeText(getBaseContext(), "Error: orientation was not locked", Toast.LENGTH_SHORT).show();
			break;

		case R.id.menu_unlock_or:
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
			break;
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		Intent joinScreenIntent = new Intent(games_screen.this, map_screen.class);
		Intent newGameIntent = new Intent(games_screen.this, matchmaking_screen.class);


		switch(v.getId()){

		case R.id.joinButton:

			if(gameSelected.length() > 1){

				/**
				 * TODO: Check for the game, join game
				 */

				startActivity(joinScreenIntent);
				finish();
				break;

			}
			else{
				/*if game list is empty or unable to join game*/
				Toast.makeText(getBaseContext(), "Unable to join game", Toast.LENGTH_SHORT).show();
				break;
			}

		case R.id.newGameButton:
			startActivity(newGameIntent);

		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id){
		gameSelected = ((TextView)view).getText().toString();

	}

	public int getOrientation()
	{
		Display getOrient = getWindowManager().getDefaultDisplay();
		int orientation = Configuration.ORIENTATION_UNDEFINED;
		if(getOrient.getWidth()==getOrient.getHeight()){
			orientation = Configuration.ORIENTATION_SQUARE;
		} else{ 
			if(getOrient.getWidth() < getOrient.getHeight()){
				orientation = Configuration.ORIENTATION_PORTRAIT;
			}else { 
				orientation = Configuration.ORIENTATION_LANDSCAPE;
			}
		}
		return orientation;
	}

}


